package com.ivanceras.server.util;

import com.ivanceras.server.dao.DAO_Ad_column;
import com.ivanceras.server.dao.DAO_Ad_field;
import com.ivanceras.server.dao.DAO_Ad_reference;
import com.ivanceras.server.dao.DAO_Ad_tab;
import com.ivanceras.server.dao.DAO_Ad_table;
import com.ivanceras.server.dao.DAO_Ad_window;
import com.ivanceras.shared.db.EMF;
import com.ivanceras.shared.db.EntityManager;
import com.ivanceras.shared.db.Filter;
import com.ivanceras.shared.db.Order;
import com.ivanceras.shared.exception.DatabaseException;

public class WindowGenerator {
	
	private final static String genWindowDirectory = Configuration.generatedWindowDirectory;
	private final static String genWindowPackageName = Configuration.generatedWindowPackageName;
	
	/**
	 * @param windowName
	 * @throws DatabaseException
	 */
	public static void generateWindow(String windowName) throws DatabaseException{
		StringBuffer buff = new StringBuffer();
		
		buff.append("<!DOCTYPE ui:UiBinder SYSTEM \"http://dl.google.com/gwt/DTD/xhtml.ent\">");
		buff.append("\n<ui:UiBinder xmlns:ui=\"urn:ui:com.google.gwt.uibinder\"");
		buff.append("\n\txmlns:g=\"urn:import:com.google.gwt.user.client.ui\">");
		buff.append("\n\t<g:HTMLPanel>");
		
		EntityManager em = EMF.get();
		Filter[] filters = new Filter[1];
		filters[0] = new Filter("name", Filter.EQUAL, windowName);
		DAO_Ad_window win = DAO_Ad_window.cast(em.getOne(new DAO_Ad_window(), filters));
		DAO_Ad_tab[] tabs = DAO_Ad_tab.cast(em.getChildren(win, new DAO_Ad_tab(), new Order("seqno")));
		
		for(DAO_Ad_tab tab : tabs){
			System.out.println("tab: "+tab.getName());
			DAO_Ad_table table = DAO_Ad_table.cast(em.getHasOne(tab, new DAO_Ad_table()));
			buff.append("\n\t\t<div ui:field_tab=\""+table.getTablename()+"\" name=\""+tab.getName()+"\" tablevel=\""+tab.getTablevel()+"\">");
			Order[] orders = new Order[1];
			orders[0] = new Order("seqno");
			DAO_Ad_field[] fields = DAO_Ad_field.cast(em.getAll(new DAO_Ad_field(), orders, new Filter("ad_tab_id", Filter.EQUAL, tab.getAd_tab_id())));//include_tab mess as ambiguous

			for(DAO_Ad_field field : fields ){
				
				DAO_Ad_column column = DAO_Ad_column.cast(em.getHasOne(field, new DAO_Ad_column()));
//				DAO_Ad_element element = (DAO_Ad_element) em.getHasOne(column, new DAO_Ad_element());
				Filter[] filters3 = new Filter[1];
				filters3[0] = new Filter("ad_reference_id",Filter.EQUAL, column.getAd_reference_id());
				DAO_Ad_reference reference = DAO_Ad_reference.cast(em.getOne(new DAO_Ad_reference(), filters3));
				Filter[] filters4 = new Filter[1];
				filters4[0] = new Filter("ad_reference_id",Filter.EQUAL, column.getAd_reference_value_id());
				DAO_Ad_reference referenceValue = DAO_Ad_reference.cast(em.getOne(new DAO_Ad_reference(), filters4));
//				DAO_Ad_val_rule valrule = DAO_Ad_val_rule.cast(em.getHasOne(column, new DAO_Ad_val_rule()));
				buff.append("\n\t\t\t<div ui:field_field=\"field_"+column.getColumnname()+"\" name=\""+field.getName()+"\" active=\""+column.getIsactive()+"\" display=\""+field.getIsdisplayed()+"\" displayLogic=\""+field.getDisplaylogic()+"\" reference=\""+reference.getName()+"\"/>");
			}
			buff.append("\n\t\t</tab>");
		}
		em.close();
		buff.append("\n\t</g:HTMLPanel>");
		buff.append("\n</ui:UiBinder>");
		FileUtil.writeToFile(buff.toString(), genWindowDirectory, win.getName().replaceAll("[&, ]", "")+".xml");
	}

	public static void main(String[] args){
		try {
			generateWindow("Window, Tab & Field");
		} catch (DatabaseException e) {
			e.printStackTrace();
		}
	}
}
